home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / support / usersetup.c < prev   
Encoding:
C/C++ Source or Header  |  1992-09-18  |  3.3 KB  |  181 lines

  1. # include    <stdio.h>
  2. # include    <ingres.h>
  3. # include    <aux.h>
  4. # include    <opsys.h>
  5. # include    <sccs.h>
  6.  
  7. SCCSID(@(#)usersetup.c    8.2    1/18/85)
  8.  
  9. /*
  10. **  Initialize Users File From Passwd File
  11. **
  12. **    Everyone in /etc/passwd is entered into the users file.  All
  13. **    users can access all databases.
  14. **
  15. **    User codes are assigned sequentially.  This should therefore
  16. **    probably be run once only, when INGRES is first installed.
  17. **    Otherwise, usercodes can change mysteriously.
  18. **
  19. **    The optional parameter replaces the root of the INGRES subtree
  20. **    as found in /etc/passwd.  The INGRES user must be installed
  21. **    (with that name) when usersetup is run.  If this parameter
  22. **    is a minus ("-"), output goes to the standard output.
  23. **
  24. **    The initialization file is initialized to "<home>/.ingres",
  25. **    where <home> is the home directory in the passwd file.
  26. */
  27.  
  28. extern char *ztack();
  29.  
  30. main(argc, argv)
  31. int    argc;
  32. char    **argv;
  33. {
  34.     register int    i;
  35.     char        buf[MAXLINE + 1];
  36.     char        *pathname;
  37.     register char    *code;
  38.     char        *field[UF_NFIELDS];
  39.     register FILE    *iop;
  40.     extern char    *Proc_name;
  41.     char        *stat = "000001";
  42.     extern    void    (*ExitFn)();
  43.     extern    void    sysexit();
  44.  
  45.     Proc_name = "USERSETUP";
  46.     ExitFn = sysexit;
  47.     pathname = NULL;
  48.     if (argc > 1)
  49.     {
  50.         argc--;
  51.         stat = *++argv;
  52.     }
  53.  
  54.     code = "aa";
  55.     if ((iop = fopen("/etc/passwd", "r")) == (FILE *)NULL)
  56.         syserr(0, "cannot open /etc/passwd for reading");
  57.  
  58.     /* scan for INGRES in /etc/passwd */
  59.     while (fgets(buf, MAXLINE, iop))
  60.     {
  61.         i = decode(buf, field);
  62.         if (!sequal(USERINGRES, field[0]))
  63.             continue;
  64.         pathname = field[i - 1];
  65.  
  66.         break;
  67.     }
  68.  
  69.     /* test for INGRES entry found */
  70.     if (!pathname)
  71.         syserr(0, "USERINGRES not installed as UNIX user");
  72.  
  73.     /* get override pathname */
  74.     if (argc > 1)
  75.         pathname = argv[1];
  76.  
  77.     /* rewind passwd file */
  78.     if (fclose(iop))
  79.         syserr("fclose");
  80.     if ((iop = fopen("/etc/passwd", "r")) == (FILE *)NULL)
  81.         syserr("open /etc/passwd 2");
  82.  
  83.     /* open output file as needed */
  84.     if (pathname[0] != '-')
  85.     {
  86.         concat(pathname, "/files/users", buf);
  87.         if ((i = open(buf, O_RDONLY)) >= 0)
  88.             syserr(0, "%s already exists", buf);
  89.         if ((i = creat(buf, 0644)) < 0)
  90.             syserr("Cannot create %s", buf);
  91.         close(i);
  92.         if (freopen(buf, "w", stdout) == NULL)
  93.             syserr("cannot open %s", buf);
  94.     }
  95.  
  96.     while (fgets(buf, MAXLINE, iop))
  97.     {
  98.         i = decode(buf, field);
  99.         /* print username & code */
  100.         printf("%s:%s:%s:%s:%s:::%s/.ingres::\n",
  101.             field[0],    /* user name */
  102.             code,
  103.             field[2],    /* user id */
  104.             field[3],    /* user group */
  105.             sequal(field[0], USERINGRES) ? "177777" : stat,
  106.             field[i - 1]);    /* working directory */
  107.         next(code);
  108.     }
  109.     fflush(stdout);
  110. }
  111. /*
  112. **  DECODE
  113. */
  114.  
  115. decode(buf, field)
  116. char    *buf;
  117. char    *field[];
  118. {
  119.     register char    *cp, c;
  120.     register int    i;
  121.  
  122.     field[0] = buf;
  123.     for (i = 0, cp = buf; (c = *cp) != '\n' && c != '\0'; cp++)
  124.     {
  125.         if (c == ':')
  126.         {
  127.             *cp = '\0';
  128.             i++;
  129.             field[i] = cp + 1;
  130.         }
  131.     }
  132.  
  133.     return (i);
  134. }
  135. /*
  136. **  NEXT -- return successor to code.
  137. */
  138.  
  139. next(code)
  140. char    code[2];
  141. {
  142.     register char    *c;
  143.     register char    a, b;
  144.  
  145.     c = code;
  146.     a = c[0];
  147.     b = c[1];
  148.  
  149.     if (++b > 'z')
  150.     {
  151.         b = '0';
  152.     }
  153.     else if (b == '9' + 1)
  154.     {
  155.         b = 'a';
  156.         if (a == 'Z')
  157.         {
  158.             write(2, "Too many users\n", 15);
  159.             exit(-1);
  160.         }
  161.         if (++a > 'z')
  162.         {
  163.             a = 'A';
  164.         }
  165.     }
  166.  
  167.     c[0] = a;
  168.     c[1] = b;
  169. }
  170.  
  171. /*
  172. ** sysexit
  173. **    A simple function that just exits, this is for the benefit
  174. **    of syserr, so it does not core dump.
  175. */
  176. sysexit(value)
  177. int    value;
  178. {
  179.     exit(value);
  180. }
  181.